package com.onlinemusic.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.onlinemusic.common.JwtUtils;
import com.onlinemusic.common.R;
import com.onlinemusic.common.Token;
import com.onlinemusic.pojo.domain.Consumer;
import com.onlinemusic.pojo.request.ConsumerRequest;
import com.onlinemusic.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user")
public class ConsumerController {
    @Autowired
    private ConsumerService consumerService;

    /**
     * 分页查询所有用户
     *
     * @param curPage
     * @param pageSize
     * @return
     */
    @GetMapping
    public R allUser(@RequestParam(defaultValue = "1") int curPage, @RequestParam(defaultValue = "5") int pageSize) {
        IPage<Consumer> page = new Page<>();
        //设置当前页
        page.setCurrent(curPage);
        //设置每页显示数据条数
        page.setSize(pageSize);
        return consumerService.allUser(page);
    }

    /**
     * 删除用户 注销账户
     *
     * @param id
     * @return
     */
    @GetMapping("delete")
    public R deleteUser(@RequestParam int id) {
        return consumerService.deleteUser(id);
    }

    /**
     * 管理端批量删除用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("deleteIds")
    public R deleteUsers(@RequestParam String[] id) {
        return consumerService.deleteUsers(id);
    }

    /**
     * 返回指定id的user
     *
     * @param id
     * @return
     */
    @GetMapping("detail")
    public R userOfId(@RequestParam int id) {
        return consumerService.userOfId(id);
    }

    /**
     * 服务端查询所有用户数量 用于info页展示
     *
     * @return
     */
    @GetMapping("users")
    public R getAllUser() {
        return consumerService.allUsers();
    }

    /**
     * 客户端用户登录判断
     * @param loginRequest
     * @param session
     * @return
     */
    @PostMapping("login/status")
    public R loginStatus(@RequestBody ConsumerRequest loginRequest, HttpSession session) {
        R result = consumerService.loginStatus(loginRequest, session);
        Map<String ,Object> map = new HashMap<>();
        map.put("res",result.getData());
        if (result.getSuccess()){
            //生成token
            String tokenStr = JwtUtils.generateToken(loginRequest.getUsername());
            Token token = new Token();
            token.setAuthorization(tokenStr);
            //放入data
            map.put("token",token);
        }
        result.setData(map);
        return result;
    }

    /**
     * 用户注册
     * @param registryRequest
     * @return
     */
    @PostMapping("add")
    public R addUser(@RequestBody ConsumerRequest registryRequest) {
        return consumerService.addUser(registryRequest);
    }

    /**
     * 更新用户资料
     * @param updateRequest
     * @return
     */
    @PostMapping("update")
    public R updateUserMsg(@RequestBody ConsumerRequest updateRequest) {
        return consumerService.updateUserMsg(updateRequest);
    }

    /**
     * 客户端更新用户头像
     * @param avatarFile
     * @param id
     * @return
     */
    @PostMapping("avatar/update")
    public R updateUserPic(@RequestParam("file") MultipartFile avatarFile, @RequestParam("id") int id) {
        return consumerService.updateUserAvatar(avatarFile, id);
    }

    /**
     * 客户端用户修改密码
     * @param updatePasswordRequest
     * @return
     */
    @PostMapping("updatePassword")
    public R updatePassword(@RequestBody ConsumerRequest updatePasswordRequest) {
        return consumerService.updatePassword(updatePasswordRequest);
    }
}
